<!DOCTYPE html>
<html lang="zh-CN"><head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>上传晒单  - {php echo $this->module['config']['sname']}</title>
    {php echo register_jssdk();} 
    <meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no, width=device-width">
	<meta content="telephone=no" name="format-detection">
    <link href="{WELIAM_INDIANA_STATIC}css/common.css" rel="stylesheet">
    <link href="{WELIAM_INDIANA_STATIC}css/user.css" rel="stylesheet">
    <link href="{WELIAM_INDIANA_STATIC}css/index.css" rel="stylesheet">
    <script src="{WELIAM_INDIANA_STATIC}js/jquery.js"></script>
</head>
{template 'common/wxshare'}
<body>
<div class="m-user" id="pro-view-5"  style="padding-top:37px;">
	
    <div class="m-simpleHeader" data-pro="dvHeader" id="pro-view-17">
        <a href="{php echo $this->createMobileUrl('person');}" data-pro="cancel" class="m-simpleHeader-back">取消</a>
                <a href="javascript:store();" data-pro="ok" class="m-simpleHeader-ok">保存</a>
        <h1>我的晒单</h1>
    </div>
    
    <div class="m-user-addrAdd">
        <div class="m-user-bar" data-pro="entry">
        	
            <div class="w-inputBar w-bar" id="pro-view-6">
                <div class="w-bar-label">标题</div>
                <div class="w-bar-control"><input data-pro="input" class="w-bar-input" type="text" id="title"></div>
            </div>
            
            <div class="w-inputBar w-bar" id="pro-view-15">
                <div class="w-bar-label">评价</div>
                <div class="w-bar-control"><textarea class="w-bar-input" id="detail"></textarea></div>
            </div>
            
            <div class="w-bar m-user-bar-margin m-user-bar-border"  id="pro-view-175">
   				<input name="" type="button" style="margin-right:10px;" value="选择图片" onclick="javascript:select_img()" />
     			<input name="" type="button"  value="上传图片" onclick="javascript:upload_img()"/>
            </div>
            
            <div class="m-index-mod m-index-popular">
                <div class="m-index-mod-bd">
                    <ul class="w-goodsList w-goodsList-s m-index-popular-list" id="img_s"></ul>                    
                </div>
            </div>
            
        </div>
    </div>
</div>
{template 'common/common_message'}
<script>
	//定义全局变量localIds选择图片array，serverId上传图片服务器id
	var localIds = new Array();
	var serverIds = new Array();
	
	//选择相册图片并且展示
	function select_img(){
		wx.chooseImage({
			count: 3, // 默认9
   		 success: function (res) {
        	localIds = res.localIds; // 返回选定照片的本地ID列表，localId可以作为img标签的src属性显示图片 
        	document.getElementById("img_s").innerHTML = "";
        	for(i=0;i<localIds.length;i++){
        		$("#img_s").append("<li class='w-goodsList-item'><div class='w-goods w-goods-ing' ><div class='w-goods-pic'><img src='"+localIds[i]+"'></div></div></li>");
        	}
        	 
 		   }
		});	
	}
	
	
	//图片上传
	var img_num=0;
	function upload_img(){
		if(localIds.length == 0){
			tip("请添加图片！",true);
			return false;
		}
			wx.uploadImage({
				    localId: localIds[img_num].toString(), // 需要上传的图片的本地ID，由chooseImage接口获得
				    isShowProgressTips: 1, // 默认为1，显示进度提示
				    success: function (res) {
				       serverIds[img_num] = res.serverId; // 返回图片的服务器端ID
				       if(img_num<localIds.length){
				       		img_num++;
				       		if(img_num>=localIds.length){
				       			img_num=0;
				       			tip("上传完毕，请保存!",true);
				       		}else{
				       			upload_img();
				       		}
				       		
				       }
				    }
				});           

            }
	
	function store(){			
		var title = $('#title').val();
		var detail = $('#detail').val();
		//设置最大三张照片
		var mediaid1 = '';
		var mediaid2 = '';
		var mediaid3 = '';
		var length = serverIds.length;
		var period_number = "{$result['period_number']}";
		
		if(!title||title.length>10){
			tip("请输入长度小于10的标题",true);
			return false;
		}
		if(!detail||detail.length>230){
			tip("请输入长度小于220的评价",true);
			return false;
		}
		if(serverIds.length == 0){
			tip("请传入不少于1张图片！",true);
			return false;
		}
		
		if(serverIds.length == 3){
			mediaid1=serverIds[0];
			mediaid2=serverIds[1];
			mediaid3=serverIds[2];
		}else if(serverIds.length == 2){
			mediaid1=serverIds[0];
			mediaid2=serverIds[1];
		}else{
			mediaid1=serverIds[0];
		}
			$.post(
	       	"{php echo $this->createMobileUrl('mysharestore',array(goodsid => $result['goodsid'], period_number=>$result['period_number'], goodstitle =>$result['goodstitle']))}",
	      		 {
	      		 	title:title,
	      		 	detail:detail,
	      		 	mediaid1:mediaid1,
	      		 	mediaid2:mediaid2,
	      		 	mediaid3:mediaid3,
	      		 	length:length,
	      		 	period_number:period_number
	      		 },function(m){
                if(m=='true'){
                  tip('提交成功,待审核！',true);
                   location.href='{php echo $this->createMobileUrl('person');}';
                }else{
                	tip(m+"dj",true);
                	 location.href='{php echo $this->createMobileUrl('person');}';
                }

             },"html");
		
	}
</script>
</body>
</html>
